#include <sys_config.h>
#include <types.h>
#include <basic_types.h>
#include <mediatypes.h>
#include <api/libc/printf.h>
#include <api/libc/string.h>
#include <osal/osal.h>
#ifndef NEW_DEMO_FRAME
#include <api/libpub29/lib_pub27.h>
#include <api/libpub29/lib_as.h>
#endif

#include <api/libosd/osd_lib.h>

#include "osdobjs_def.h"
#include "string.id"
#include "images.id"
#include "osd_config.h"

#include "menus_root.h"
#include "win_com.h"
#include "win_com_popup.h"
#include "win_com_list.h"
#include "win_signalstatus.h"

#include "win_signal.h"
/*******************************************************************************
*	Objects definition
*******************************************************************************/
extern CONTAINER g_win_signal;

extern PROGRESS_BAR sig_bar_level;
extern PROGRESS_BAR sig_bar_quality;
extern TEXT_FIELD	 sig_txt_level;
extern TEXT_FIELD	 sig_txt_quality;
extern TEXT_FIELD	 sig_txt_level_per;
extern TEXT_FIELD	 sig_txt_quality_per;

#ifndef SD_UI
#if Maker_GUI_ON
#ifdef	_BUILD_OTA_E_
#define	W_L     80
#else
#define	W_L     110
#endif
#else
#ifdef	_BUILD_OTA_E_
#define	W_L     64
#else
#define	W_L     110
#endif
#endif
#define	W_L_B   (W_L + 16)
#if Maker_GUI_ON
#define	W_T     255
#else
#define	W_T     310
#endif
#define	W_W     630
#define	W_W_B   (W_W - 16)
#define	W_H     86

#define TXT_H			40//26
#define TXTL_L_OF 		16
#define TXTL_L_OF_B 	(TXTL_L_OF + 16)

#define TXTLL_T_OF 	4
#define TXTLQ_T_OF 	(TXTLL_T_OF + TXT_H + 2)
#define TXTL_W			250

#define TXTR_L_OF 		(TXTL_L_OF + TXTL_W + B_W + 10)
#define TXTRL_T_OF 	4
#define TXTRQ_T_OF 	(TXTLL_T_OF + TXT_H + 2)
#define TXTR_W			80

#define B_L_OF	 (TXTL_L_OF + TXTL_W)
#define B_L_OF_B	 (B_L_OF + 16)
#define B_H		 24 //24 //16
#define B_W		 270
#define B_W_B		 (B_W - 16)
#define BL_T_OF	 (TXTLL_T_OF + (TXT_H - B_H)/2)
#define BQ_T_OF	 (BL_T_OF + TXT_H + 2)
#define MINI_SIGNAL_L 62
#define MINI_SIGNAL_T 210
#else
#ifdef	_BUILD_OTA_E_
#define	W_L     64
#define  TXTL_W			140//250
#define  TXTR_W			40//80
#else
#define	W_L     50//110
#define  TXTL_W			150//250
#define  TXTR_W			50//80
#endif
#define	W_L_B   (W_L + 16)
#define	W_T     300
#define	W_W     450
#define	W_W_B   (W_W - 16)
#define	W_H     60

#define TXT_H			24//26
#define TXTL_L_OF 		16
#define TXTL_L_OF_B 	(TXTL_L_OF + 16)

#define TXTLL_T_OF 	4
#define TXTLQ_T_OF 	(TXTLL_T_OF + TXT_H + 2)


#define TXTR_L_OF 		400//(TXTL_L_OF + TXTL_W + B_W + 20)
#define TXTRL_T_OF 	4
#define TXTRQ_T_OF 	(TXTLL_T_OF + TXT_H + 2)


#define B_L_OF	 (TXTL_L_OF + TXTL_W)
#define B_L_OF_B	 (B_L_OF + 16)
#define B_H		 16//24 //24 //16
#define B_W		 210
#define B_W_B		 (B_W - 16)
#define BL_T_OF	 (TXTLL_T_OF + (TXT_H - B_H)/2)
#define BQ_T_OF	 (BL_T_OF + TXT_H + 2)
#define MINI_SIGNAL_L 62
#define MINI_SIGNAL_T 210

#endif

#define WIN_SH_IDX					WSTL_NOSHOW_IDX //WSTL_NOSHOW_IDX //WSTL_WIN_SIGNAL_01
#define B_BG_SH_IDX				    WSTL_BARBG_01_8BIT
#define B_LEVEL_UNLOCK_SH_IDX		WSTL_BAR_03_8BIT//WSTL_BAR_03
#define B_LEVEL_LOCK_SH_IDX		    WSTL_BAR_01_8BIT//WSTL_BAR_01
#define B_QUALITY_UNLOCK_SH_IDX	    WSTL_BAR_03_8BIT//WSTL_BAR_03
#define B_QUALITY_LOCK_SH_IDX	    WSTL_BAR_02_8BIT//WSTL_BAR_02
#define B_MID_SH_IDX				WSTL_NOSHOW_IDX
#define TXT_SH_IDX					WSTL_TEXT_30_8BIT //WSTL_MIXBACK_IDX_06_8BIT //WSTL_NOSHOW_IDX //WSTL_MIXBACK_BLACK_IDX

#define WIN_SH_IDX_HD					WSTL_NOSHOW_IDX//WSTL_NOSHOW_IDX //WSTL_NOSHOW_IDX //WSTL_WIN_SIGNAL_01
#define B_BG_SH_IDX_HD				    WSTL_BARBG_01_HD
#define B_LEVEL_UNLOCK_SH_IDX_HD		WSTL_BAR_03_HD//WSTL_BAR_03
#define B_LEVEL_LOCK_SH_IDX_HD		    WSTL_BAR_01_HD//WSTL_BAR_01
#define B_QUALITY_UNLOCK_SH_IDX_HD	    WSTL_BAR_03_HD//WSTL_BAR_03
#define B_QUALITY_LOCK_SH_IDX_HD	    WSTL_BAR_02_HD//WSTL_BAR_02
#define B_MID_SH_IDX_HD				    WSTL_NOSHOW_IDX
#define TXT_SH_IDX_HD					WSTL_BUTTON_01_HD//WSTL_TEXT_30_8BIT //WSTL_MIXBACK_IDX_06_8BIT //WSTL_NOSHOW_IDX //WSTL_MIXBACK_BLACK_IDX


#if Maker_GUI_OFF
#define LDEF_PROGRESS_BAR(root,varBar,nxtObj,l,t,w,h,style,rl,rt,rw,rh,fsh)	\
	DEF_PROGRESSBAR(varBar, root, nxtObj, C_ATTR_ACTIVE, 0, \
		0, 0, 0, 0, 0, l, t, w, h, B_BG_SH_IDX, B_BG_SH_IDX, B_BG_SH_IDX, B_BG_SH_IDX,\
		NULL, NULL, style, 0, 0, B_MID_SH_IDX, fsh, \
		rl,rt , rw, rh, 1, 100, 50, 1)
#else
#define LDEF_PROGRESS_BAR(root,varBar,nxtObj,l,t,w,h,style,rl,rt,rw,rh,fsh)	\
	DEF_PROGRESSBAR(varBar, root, nxtObj, C_ATTR_ACTIVE, 0, \
		0, 0, 0, 0, 0, l, t, w, h, B_BG_SH_IDX, B_BG_SH_IDX, B_BG_SH_IDX, B_BG_SH_IDX,\
		NULL, NULL, style, 0, 0, B_MID_SH_IDX, fsh, \
		rl,rt , rw, rh, 1, 100, 100, 1)
#endif

#define LDEF_PROGRESS_BAR2(root,varBar,nxtObj,l,t,w,h,style,bx,by,rl,rt,rw,rh,fsh)	\
	DEF_PROGRESSBAR(varBar, root, nxtObj, C_ATTR_ACTIVE, 0, \
		0, 0, 0, 0, 0, l, t, w, h, B_BG_SH_IDX, B_BG_SH_IDX, B_BG_SH_IDX, B_BG_SH_IDX,\
		NULL, NULL, style, bx,by, B_MID_SH_IDX, fsh, \
		rl,rt , rw, rh, 1, 10, 10, 1)

		
#define LDEF_TXT(root,varTxt,nxtObj,l,t,w,h,resID,str)	\
	DEF_TEXTFIELD(varTxt,root,nxtObj,C_ATTR_ACTIVE,0, \
	    0,0,0,0,0, l,t,w,h, TXT_SH_IDX,TXT_SH_IDX,TXT_SH_IDX,TXT_SH_IDX,   \
	    NULL,NULL,  \
	    C_ALIGN_LEFT | C_ALIGN_VCENTER, 0,0,resID,str)

#define LDEF_WIN(varCon,nxtObj,l,t,w,h,focusID)		\
    DEF_CONTAINER(varCon,NULL,NULL,C_ATTR_ACTIVE,0, \
    0,0,0,0,0, l,t,w,h, WIN_SH_IDX,WIN_SH_IDX,WIN_SH_IDX,WIN_SH_IDX,   \
    NULL,NULL,  \
    nxtObj, focusID,0)


extern UINT16 level_percent[];
extern UINT16 quality_percent[];

#if Maker_GUI_ON
LDEF_TXT(&g_win_signal,sig_txt_level,&sig_bar_level,	\
		W_L + TXTL_L_OF,195 + TXTLL_T_OF, TXTL_W, TXT_H,RS_INFO_SIGNAL_INTENSITY,NULL)
LDEF_TXT(&g_win_signal,sig_txt_quality,&sig_bar_quality,	\
		W_L + TXTL_L_OF,195 + TXTLQ_T_OF, TXTL_W, TXT_H,RS_INFO_SIGNAL_QUALITY,NULL)

LDEF_TXT(&g_win_signal,sig_txt_level_per,&sig_txt_quality,	\
		W_L + TXTR_L_OF,195 + TXTRL_T_OF, TXTR_W, TXT_H,0,level_percent)
LDEF_TXT(&g_win_signal,sig_txt_quality_per,NULL ,	\
		W_L + TXTR_L_OF,195 + TXTRQ_T_OF, TXTR_W, TXT_H,0,quality_percent)
		
#ifndef SD_UI
	LDEF_PROGRESS_BAR(&g_win_signal,sig_bar_level,&sig_txt_level_per,	\
			W_L + B_L_OF, 195 + BL_T_OF, B_W, B_H, PROGRESSBAR_HORI_NORMAL| PBAR_STYLE_RECT_STYLE,2,5,B_W,B_H-10,0)
	//LDEF_PROGRESS_BAR2(&g_win_signal,sig_bar_level,&sig_txt_level_per,	\
	//	W_L + B_L_OF, W_T + BL_T_OF, B_W, B_H, PROGRESSBAR_HORI_REVERSE| PBAR_STYLE_RECT_STYLE,1,0,0,5,B_W,B_H-10,0)
	
	LDEF_PROGRESS_BAR(&g_win_signal,sig_bar_quality,&sig_txt_quality_per,	\
			W_L + B_L_OF, 195 + BQ_T_OF, B_W, B_H, PROGRESSBAR_HORI_NORMAL | PBAR_STYLE_RECT_STYLE,2,5,B_W,B_H-10,0)
	//LDEF_PROGRESS_BAR2(&g_win_signal,sig_bar_quality,&sig_txt_quality_per,	\
	//		W_L + B_L_OF, W_T + BQ_T_OF, B_W, B_H, PROGRESSBAR_HORI_NORMAL| PBAR_STYLE_RECT_STYLE,1,0,0,5,B_W,B_H-10,0)
#else
	LDEF_PROGRESS_BAR(&g_win_signal,sig_bar_level,&sig_txt_level_per,	\
			W_L + B_L_OF, 195 + BL_T_OF, B_W, B_H, PROGRESSBAR_HORI_NORMAL| PBAR_STYLE_RECT_STYLE,2,2,B_W,B_H-4,0)
	//LDEF_PROGRESS_BAR2(&g_win_signal,sig_bar_level,&sig_txt_level_per,	\
	//	W_L + B_L_OF, W_T + BL_T_OF, B_W, B_H, PROGRESSBAR_HORI_REVERSE| PBAR_STYLE_RECT_STYLE,1,0,0,5,B_W,B_H-10,0)
	
	LDEF_PROGRESS_BAR(&g_win_signal,sig_bar_quality,&sig_txt_quality_per,	\
			W_L + B_L_OF, 195 + BQ_T_OF, B_W, B_H, PROGRESSBAR_HORI_NORMAL | PBAR_STYLE_RECT_STYLE,2,2,B_W,B_H-4,0)
	//LDEF_PROGRESS_BAR2(&g_win_signal,sig_bar_quality,&sig_txt_quality_per,	\
	//		W_L + B_L_OF, W_T + BQ_T_OF, B_W, B_H, PROGRESSBAR_HORI_NORMAL| PBAR_STYLE_RECT_STYLE,1,0,0,5,B_W,B_H-10,0)
	
#endif
#else
LDEF_TXT(&g_win_signal,sig_txt_level,&sig_bar_level,	\
		W_L + TXTL_L_OF,W_T + TXTLL_T_OF, TXTL_W, TXT_H,RS_INFO_SIGNAL_INTENSITY,NULL)
LDEF_TXT(&g_win_signal,sig_txt_quality,&sig_bar_quality,	\
		W_L + TXTL_L_OF,W_T + TXTLQ_T_OF, TXTL_W, TXT_H,RS_INFO_SIGNAL_QUALITY,NULL)

LDEF_TXT(&g_win_signal,sig_txt_level_per,&sig_txt_quality,	\
		W_L + TXTR_L_OF,W_T + TXTRL_T_OF, TXTR_W, TXT_H,0,level_percent)
LDEF_TXT(&g_win_signal,sig_txt_quality_per,NULL ,	\
		W_L + TXTR_L_OF,W_T + TXTRQ_T_OF, TXTR_W, TXT_H,0,quality_percent)

#ifndef SD_UI
LDEF_PROGRESS_BAR(&g_win_signal,sig_bar_level,&sig_txt_level_per,	\
		W_L + B_L_OF, W_T + BL_T_OF, B_W, B_H, PROGRESSBAR_HORI_NORMAL| PBAR_STYLE_RECT_STYLE,2,5,B_W,B_H-10,0)
//LDEF_PROGRESS_BAR2(&g_win_signal,sig_bar_level,&sig_txt_level_per,	\
//	W_L + B_L_OF, W_T + BL_T_OF, B_W, B_H, PROGRESSBAR_HORI_REVERSE| PBAR_STYLE_RECT_STYLE,1,0,0,5,B_W,B_H-10,0)

LDEF_PROGRESS_BAR(&g_win_signal,sig_bar_quality,&sig_txt_quality_per,	\
		W_L + B_L_OF, W_T + BQ_T_OF, B_W, B_H, PROGRESSBAR_HORI_NORMAL | PBAR_STYLE_RECT_STYLE,2,5,B_W,B_H-10,0)
//LDEF_PROGRESS_BAR2(&g_win_signal,sig_bar_quality,&sig_txt_quality_per,	\
//		W_L + B_L_OF, W_T + BQ_T_OF, B_W, B_H, PROGRESSBAR_HORI_NORMAL| PBAR_STYLE_RECT_STYLE,1,0,0,5,B_W,B_H-10,0)
#else
LDEF_PROGRESS_BAR(&g_win_signal,sig_bar_level,&sig_txt_level_per,	\
		W_L + B_L_OF, W_T + BL_T_OF, B_W, B_H, PROGRESSBAR_HORI_NORMAL| PBAR_STYLE_RECT_STYLE,2,2,B_W,B_H-4,0)
//LDEF_PROGRESS_BAR2(&g_win_signal,sig_bar_level,&sig_txt_level_per,	\
//	W_L + B_L_OF, W_T + BL_T_OF, B_W, B_H, PROGRESSBAR_HORI_REVERSE| PBAR_STYLE_RECT_STYLE,1,0,0,5,B_W,B_H-10,0)

LDEF_PROGRESS_BAR(&g_win_signal,sig_bar_quality,&sig_txt_quality_per,	\
		W_L + B_L_OF, W_T + BQ_T_OF, B_W, B_H, PROGRESSBAR_HORI_NORMAL | PBAR_STYLE_RECT_STYLE,2,2,B_W,B_H-4,0)
//LDEF_PROGRESS_BAR2(&g_win_signal,sig_bar_quality,&sig_txt_quality_per,	\
//		W_L + B_L_OF, W_T + BQ_T_OF, B_W, B_H, PROGRESSBAR_HORI_NORMAL| PBAR_STYLE_RECT_STYLE,1,0,0,5,B_W,B_H-10,0)

#endif
#endif

LDEF_WIN(g_win_signal,&sig_txt_level,W_L,W_T,W_W,W_H,0)


/*******************************************************************************
*	Local vriable & function declare
*******************************************************************************/
UINT16 level_percent[10];
UINT16 quality_percent[10];
UINT8 i = 0; //Martin@20150113_sugnal_quality
UINT8 qualitybuf[5]; //Martin@20150113_sugnal_quality

/*******************************************************************************
*	key mapping and event callback definition
*******************************************************************************/
#ifdef WIFI_SUPPORT
BOOL win_signal_set_wifi_level_quality(UINT32 level,UINT32 quality,UINT32 lock)
{
	PROGRESS_BAR* bar;
	POBJECT_HEAD pObj;
	UINT16 level_shIdx;
    UINT16 quality_shIdx;


    if(lock)
    {
    	level_shIdx  = B_LEVEL_LOCK_SH_IDX;	
        quality_shIdx = B_QUALITY_LOCK_SH_IDX;
    }
    else
    {
    	level_shIdx = B_LEVEL_UNLOCK_SH_IDX;
        quality_shIdx = B_QUALITY_UNLOCK_SH_IDX;
    }	
    if(level < 4)
	    level_shIdx = WSTL_NOSHOW_IDX;
    if(quality < 4)
	   quality_shIdx = WSTL_NOSHOW_IDX;
    
	bar = &sig_bar_level;
	OSD_SetProgressBarPos(bar,level);
	bar->wTickFg = level_shIdx;
	
	bar = &sig_bar_quality;


	OSD_SetProgressBarPos(bar,quality);
	bar->wTickFg = quality_shIdx;

	/* If signal display is on */
	pObj = (POBJECT_HEAD)&g_win_signal;
	if(pObj->pRoot == NULL)
		return FALSE;
	else
		return TRUE;
}
#endif

/*Martin@20150113_signal_quality*/
void nsort(int array[], int size)
{
    int i, j, temp;

    for (i = 0; i < size - 1; i++) {
        for (j = 1; j < size; j++) {
            if (array[j - 1] > array[j]) {
                temp = array[j - 1];
                array[j - 1] = array[j];
                array[j] = temp;
            }
        }
    }
}
//--End Martin

BOOL win_signal_set_level_quality(UINT32 level,UINT32 quality,UINT32 lock)
{
	PROGRESS_BAR* bar;
	POBJECT_HEAD pObj;
	UINT16 level_shIdx;
    UINT16 quality_shIdx;
	static UINT32 pre_quality=0; //Martin@20150113_signal_quality

    if(lock)
    {
    	level_shIdx  = B_LEVEL_LOCK_SH_IDX;	
        quality_shIdx = B_QUALITY_LOCK_SH_IDX;
    }
    else
    {
    	level_shIdx = B_LEVEL_UNLOCK_SH_IDX;
        quality_shIdx = B_QUALITY_UNLOCK_SH_IDX;
    }	

    /*Martin@20150113_signal_quality*/
    if(lock ==0)
    {
    	pre_quality = 0;//Martin@20150113_signal_quality
    	//quality =0;
    	level = 0;
    }
    else
    {
    	if(pre_quality == 0 && quality != 0 && quality != 100 )
    	{
    		if(quality <= 30 && quality != 0)
    		{
    			pre_quality = (quality / 2) + 40;
    		}
    		else if (quality > 30)
    		{
    			pre_quality = (quality / 2) + 50;
    	    }
    	}else if(pre_quality == 0 && quality == 0 )
    	{
    		if(level <= 34)
    		{
    			pre_quality = 0;
    		}
    	}
    	if(i != 5)
    	{
    		if(quality == 0)
    		{

				if( level == 42)
				{
					pre_quality = 38;
				}
				else if(level < 38 )
				{
					pre_quality = 0;
				}
    		}
    		else if(quality > 0 && quality < 11)
    	    {
    	    	quality = 4;
    	    }
    	    else if(quality >= 12 && quality < 18)
    	    {
    	    	quality = 16;
    	    }
    	    else if(quality >= 18 && quality < 24)
    	    {
    	    	quality = 24;
    	    }
    	    else if(quality >= 24 && quality < 32)
    	    {
    	    	quality = 28;
    	    }
    	    else if(quality >= 32 && quality < 41)
    	    {
    	    	quality = 30;
    	    }
    	    else if(quality >= 41 && quality < 46)
    	    {
    	    	quality = 38;
    	    }
    	    else if(quality >= 46 && quality < 56)
    	    {
    	    	quality = 46;
    	    }
    	    else if(quality >= 56 && quality < 64)
    	    {
    	    	quality = 56;
    	    }
    	    else if(quality >= 64 && quality < 76)
    	    {
    	    	quality = 66;
    	    }
    	    else if(quality >= 76 && quality < 84)
    	    {
    	    	quality = 80;
    	    }
    	    else if(quality >= 84 && quality < 92)
    	    {
    	    	quality = 90;
    	    }
    	    else
    	    {
    	    	quality = 100;
    	    }
    	    qualitybuf[i++] = quality;
    	}
    	else
    	{
    		nsort(qualitybuf, sizeof(qualitybuf[0]));
    		quality = (qualitybuf[1] + qualitybuf[2] + qualitybuf[3]) / 3;

    		if(quality <= 30 && quality != 0)
    		{
    			pre_quality = (quality / 2) + 42;
    		}
    		else if (quality > 30)
    		{
    			pre_quality = (quality / 2) + 50;
    		}
    		memset(qualitybuf, 0, sizeof(qualitybuf));
    		i = 0;
    	}
    }

    if(level < 4)
      	    level_shIdx = WSTL_NOSHOW_IDX;
       //if(quality < 4)
    if(pre_quality < 4)
      	   quality_shIdx = WSTL_NOSHOW_IDX;

    //--End Martin




    bar = &sig_bar_level;
	OSD_SetProgressBarPos(bar,level);
	bar->wTickFg = level_shIdx;
	
	bar = &sig_bar_quality;

	/* if(quality < 10) //Martin@20150113_signal_quality
    {
        quality = 10;
    }
    */
	OSD_SetProgressBarPos(bar, pre_quality);

	//OSD_SetProgressBarPos(bar,quality); //Martin@20150113_signal_quality
	bar->wTickFg = quality_shIdx;


	/* If signal display is on */
	pObj = (POBJECT_HEAD)&g_win_signal;
	if(pObj->pRoot == NULL)
		return FALSE;
	else
		return TRUE;
}



void win_signal_open(POBJECT_HEAD w)
{
	POBJECT_HEAD pObj;
    UINT16 level = 0;
    UINT8 quality = 0;
    UINT8 lock = 0;

	struct nim_device* nim_dev;
    
  	if(get_tuner_cnt() == 1)
		cur_tuner_idx = 0;
    
  	if(cur_tuner_idx == 0)
		nim_dev = g_nim_dev;
	else
		nim_dev = g_nim_dev2;	
    
	if(w->bType != OT_CONTAINER)
		return ;
	
	pObj = (POBJECT_HEAD)&g_win_signal;
	pObj->pRoot = w;
	
	pObj = (POBJECT_HEAD)&sig_bar_level;
	pObj->frame.uLeft = W_L + B_L_OF;
	pObj->frame.uWidth = B_W;
	
	pObj = (POBJECT_HEAD)& sig_bar_quality;
	pObj->frame.uLeft = W_L + B_L_OF;
	pObj->frame.uWidth = B_W;
	
	pObj = (POBJECT_HEAD)&sig_txt_level;
	pObj->frame.uLeft = W_L + TXTL_L_OF;
		
	pObj = (POBJECT_HEAD)&sig_txt_quality;
	pObj->frame.uLeft = W_L + TXTL_L_OF;
	
	
	pObj = (POBJECT_HEAD)&g_win_signal;
	pObj->frame.uLeft = W_L;
	pObj->frame.uWidth = W_W;
	
   	nim_get_lock(nim_dev, &lock);
#ifdef _S3281_	
	nim_get_AGC(nim_dev, &level);
#endif	
	nim_get_SNR(nim_dev, &quality);
    if(NULL != nim_dev->do_ioctl)
    {
      nim_dev->do_ioctl(nim_dev, NIM_DRIVER_GET_AGC, (UINT32)&level);
    }

	dem_signal_to_display(lock,(UINT8*)&level,&quality);	
	win_signal_set_level_quality(level,quality,lock);	
}

void win_signal_open_ex(POBJECT_HEAD w , UINT32 x, UINT32 y)
{
	POBJECT_HEAD pObj;
    PPROGRESS_BAR pBar;
    UINT16 level = 0;
    UINT8 quality = 0;
    UINT8 lock = 0;


	struct nim_device* nim_dev;
  	if(cur_tuner_idx == 0)
		nim_dev = g_nim_dev;
	else
		nim_dev = g_nim_dev2;	

	if(w->bType != OT_CONTAINER)
		return ;
	
	pObj = (POBJECT_HEAD)&g_win_signal;
	pObj->pRoot = w;    
    OSD_SetColor(pObj,WIN_SH_IDX_HD,WIN_SH_IDX_HD,WIN_SH_IDX_HD,WIN_SH_IDX_HD);
    
	pObj = (POBJECT_HEAD)&sig_bar_level;
	//pObj->frame.uLeft = x+B_L_OF;//W_L + B_L_OF;
	pObj->frame.uWidth = B_W;
    OSD_SetColor(pObj,B_BG_SH_IDX_HD,B_BG_SH_IDX_HD,B_BG_SH_IDX_HD,B_BG_SH_IDX_HD);
    pBar = (PPROGRESS_BAR)&sig_bar_level;
    pBar->wTickBg = B_MID_SH_IDX_HD;
    //libc_printf("bar:%d,%d\n",B_BG_SH_IDX_HD,B_MID_SH_IDX_HD);
    
	pObj = (POBJECT_HEAD)& sig_bar_quality;
	//pObj->frame.uLeft = x+ B_L_OF;//W_L + B_L_OF;
	pObj->frame.uWidth = B_W;
    OSD_SetColor(pObj,B_BG_SH_IDX_HD,B_BG_SH_IDX_HD,B_BG_SH_IDX_HD,B_BG_SH_IDX_HD);
    pBar = (PPROGRESS_BAR)&sig_bar_quality;
    pBar->wTickBg = B_MID_SH_IDX_HD;
    
	pObj = (POBJECT_HEAD)&sig_txt_level;
	//pObj->frame.uLeft = x+TXTL_L_OF;//W_L + TXTL_L_OF;
    OSD_SetColor(pObj,TXT_SH_IDX_HD,TXT_SH_IDX_HD,TXT_SH_IDX_HD,TXT_SH_IDX_HD);
    //libc_printf("txt:%d\n",TXT_SH_IDX_HD);
       
	pObj = (POBJECT_HEAD)&sig_txt_quality;
	//pObj->frame.uLeft = x + TXTL_L_OF;//W_L + TXTL_L_OF;
	OSD_SetColor(pObj,TXT_SH_IDX_HD,TXT_SH_IDX_HD,TXT_SH_IDX_HD,TXT_SH_IDX_HD);

    pObj = (POBJECT_HEAD)&sig_txt_level_per;
    //pObj->frame.uLeft = x + TXTR_L_OF;//W_L + TXTL_L_OF;
    //pObj->frame.uTop = y + TXTRL_T_OF;
	OSD_SetColor(pObj,TXT_SH_IDX_HD,TXT_SH_IDX_HD,TXT_SH_IDX_HD,TXT_SH_IDX_HD);

    pObj = (POBJECT_HEAD)&sig_txt_quality_per;
    //pObj->frame.uLeft = x + TXTR_L_OF;//W_L + TXTL_L_OF;
    //pObj->frame.uTop = y + TXTRQ_T_OF;
	OSD_SetColor(pObj,TXT_SH_IDX_HD,TXT_SH_IDX_HD,TXT_SH_IDX_HD,TXT_SH_IDX_HD);
    
    
	pObj = (POBJECT_HEAD)&g_win_signal;
	//pObj->frame.uLeft = W_L;
    //pObj->frame.uTop = W_T;
	//pObj->frame.uTop = y;
	pObj->frame.uWidth = W_W;
	OSD_MoveObject(pObj,x,y,0);
     
    nim_get_lock(nim_dev, &lock);
#ifdef _S3281_	
	nim_get_AGC(nim_dev, &level);
#endif	
	
 	nim_get_SNR(nim_dev, &quality);

    
    if(NULL != nim_dev->do_ioctl)
    {
      nim_dev->do_ioctl(nim_dev, NIM_DRIVER_GET_AGC, (UINT32)&level);
    }

    if(!lock)
    {
        level = 0;
        quality = 0;
    }
    
	dem_signal_to_display(lock,(UINT8*)&level,&quality);	    
 	win_signal_set_level_quality(level,quality,lock);	    

}

void win_minisignal_open(POBJECT_HEAD w)
{
	POBJECT_HEAD pObj;
    PPROGRESS_BAR pBar;
    UINT16 level = 0;
    UINT8 quality = 0;
    UINT8 lock = 0;


	struct nim_device* nim_dev;
  	if(cur_tuner_idx == 0)
		nim_dev = g_nim_dev;
	else
		nim_dev = g_nim_dev2;	

	if(w->bType != OT_CONTAINER)
		return ;
	
	pObj = (POBJECT_HEAD)&g_win_signal;
	pObj->pRoot = w;
    //OSD_SetColor(pObj,WIN_SH_IDX,WIN_SH_IDX,WIN_SH_IDX,WIN_SH_IDX);
    
	pObj = (POBJECT_HEAD)&sig_bar_level;
	//pObj->frame.uLeft = W_L + B_L_OF;
	pObj->frame.uWidth = B_W;
    OSD_SetColor(pObj,B_BG_SH_IDX,B_BG_SH_IDX,B_BG_SH_IDX,B_BG_SH_IDX);
    pBar = (PPROGRESS_BAR)&sig_bar_level;
    pBar->wTickBg = B_MID_SH_IDX;

    
	pObj = (POBJECT_HEAD)& sig_bar_quality;
	//pObj->frame.uLeft = W_L + B_L_OF;
	pObj->frame.uWidth = B_W;
    OSD_SetColor(pObj,B_BG_SH_IDX,B_BG_SH_IDX,B_BG_SH_IDX,B_BG_SH_IDX);
    pBar = (PPROGRESS_BAR)&sig_bar_quality;
    pBar->wTickBg = B_MID_SH_IDX;

    
	pObj = (POBJECT_HEAD)&sig_txt_level;
	//pObj->frame.uLeft = W_L + TXTL_L_OF;
    OSD_SetColor(pObj, TXT_SH_IDX, TXT_SH_IDX, TXT_SH_IDX, TXT_SH_IDX);
    
	pObj = (POBJECT_HEAD)&sig_txt_quality;
	//pObj->frame.uLeft = W_L + TXTL_L_OF;
    OSD_SetColor(pObj, TXT_SH_IDX, TXT_SH_IDX, TXT_SH_IDX, TXT_SH_IDX);
    
	pObj = (POBJECT_HEAD)&sig_txt_level_per;
    OSD_SetColor(pObj, TXT_SH_IDX, TXT_SH_IDX, TXT_SH_IDX, TXT_SH_IDX);
    
	pObj = (POBJECT_HEAD)&sig_txt_quality_per;
    OSD_SetColor(pObj, TXT_SH_IDX, TXT_SH_IDX, TXT_SH_IDX, TXT_SH_IDX);
    
	pObj = (POBJECT_HEAD)&g_win_signal;
	//pObj->frame.uLeft = W_L;
	//pObj->frame.uWidth = W_W;
	OSD_SetColor(pObj, TXT_SH_IDX, TXT_SH_IDX, TXT_SH_IDX, TXT_SH_IDX);

    
	OSD_MoveObject(pObj,MINI_SIGNAL_L, MINI_SIGNAL_T,FALSE);

   	nim_get_lock(nim_dev, &lock);
#ifdef _S3281_	
	nim_get_AGC(nim_dev, &level);
#endif	
	nim_get_SNR(nim_dev, &quality);
    if(NULL != nim_dev->do_ioctl)
    {
      nim_dev->do_ioctl(nim_dev, NIM_DRIVER_GET_AGC, (UINT32)&level);
    }

	dem_signal_to_display(lock,(UINT8*)&level,&quality);	
	win_signal_set_level_quality(level,quality,lock);	
}

void win_minisignal_open_ex(POBJECT_HEAD w,UINT32 x, UINT32 y)
{
	POBJECT_HEAD pObj;
    PPROGRESS_BAR pBar;
    UINT16 level = 0;
    UINT8 quality = 0;
    UINT8 lock = 0;


	struct nim_device* nim_dev;
  	if(cur_tuner_idx == 0)
		nim_dev = g_nim_dev;
	else
		nim_dev = g_nim_dev2;	

	if(w->bType != OT_CONTAINER)
		return ;
	
	pObj = (POBJECT_HEAD)&g_win_signal;
	pObj->pRoot = w;
    //OSD_SetColor(pObj,WIN_SH_IDX,WIN_SH_IDX,WIN_SH_IDX,WIN_SH_IDX);
    
	pObj = (POBJECT_HEAD)&sig_bar_level;
	//pObj->frame.uLeft = W_L + B_L_OF;
	pObj->frame.uWidth = B_W;
    OSD_SetColor(pObj,B_BG_SH_IDX,B_BG_SH_IDX,B_BG_SH_IDX,B_BG_SH_IDX);
    pBar = (PPROGRESS_BAR)&sig_bar_level;
    pBar->wTickBg = B_MID_SH_IDX;

    
	pObj = (POBJECT_HEAD)& sig_bar_quality;
	//pObj->frame.uLeft = W_L + B_L_OF;
	pObj->frame.uWidth = B_W;
    OSD_SetColor(pObj,B_BG_SH_IDX,B_BG_SH_IDX,B_BG_SH_IDX,B_BG_SH_IDX);
    pBar = (PPROGRESS_BAR)&sig_bar_quality;
    pBar->wTickBg = B_MID_SH_IDX;

    
	pObj = (POBJECT_HEAD)&sig_txt_level;
	//pObj->frame.uLeft = W_L + TXTL_L_OF;
    OSD_SetColor(pObj, TXT_SH_IDX, TXT_SH_IDX, TXT_SH_IDX, TXT_SH_IDX);
    
	pObj = (POBJECT_HEAD)&sig_txt_quality;
	//pObj->frame.uLeft = W_L + TXTL_L_OF;
    OSD_SetColor(pObj, TXT_SH_IDX, TXT_SH_IDX, TXT_SH_IDX, TXT_SH_IDX);
    
	pObj = (POBJECT_HEAD)&sig_txt_level_per;
    OSD_SetColor(pObj, TXT_SH_IDX, TXT_SH_IDX, TXT_SH_IDX, TXT_SH_IDX);
    
	pObj = (POBJECT_HEAD)&sig_txt_quality_per;
    OSD_SetColor(pObj, TXT_SH_IDX, TXT_SH_IDX, TXT_SH_IDX, TXT_SH_IDX);
    
	pObj = (POBJECT_HEAD)&g_win_signal;
	//pObj->frame.uLeft = W_L;
	//pObj->frame.uWidth = W_W;
	OSD_SetColor(pObj, TXT_SH_IDX, TXT_SH_IDX, TXT_SH_IDX, TXT_SH_IDX);

    
	//OSD_MoveObject(pObj,MINI_SIGNAL_L, MINI_SIGNAL_T,FALSE);
    OSD_MoveObject(pObj,x,y,FALSE);
   	nim_get_lock(nim_dev, &lock);
#ifdef _S3281_	
	nim_get_AGC(nim_dev, &level);
#endif	
	nim_get_SNR(nim_dev, &quality);
    if(NULL != nim_dev->do_ioctl)
    {
      nim_dev->do_ioctl(nim_dev, NIM_DRIVER_GET_AGC, (UINT32)&level);
    }

	dem_signal_to_display(lock,(UINT8*)&level,&quality);	
	win_signal_set_level_quality(level,quality,lock);	
}

void win_minisignal_restore()
{
	POBJECT_HEAD pObj;

	pObj = (POBJECT_HEAD)&g_win_signal;
	OSD_SetColor(pObj, WIN_SH_IDX, WIN_SH_IDX, WIN_SH_IDX, WIN_SH_IDX);
	OSD_MoveObject(pObj,W_L, W_T,FALSE);

	pObj = (POBJECT_HEAD)&sig_txt_level;
	OSD_SetColor(pObj, TXT_SH_IDX, TXT_SH_IDX, TXT_SH_IDX, TXT_SH_IDX);

	pObj = (POBJECT_HEAD)&sig_txt_quality;
	OSD_SetColor(pObj, TXT_SH_IDX, TXT_SH_IDX, TXT_SH_IDX, TXT_SH_IDX);

	pObj = (POBJECT_HEAD)&sig_txt_level_per;
	OSD_SetColor(pObj, TXT_SH_IDX, TXT_SH_IDX, TXT_SH_IDX, TXT_SH_IDX);

	pObj = (POBJECT_HEAD)&sig_txt_quality_per;
	OSD_SetColor(pObj, TXT_SH_IDX, TXT_SH_IDX, TXT_SH_IDX, TXT_SH_IDX);
}
void win_signal_open_tplist(POBJECT_HEAD w)
{
	POBJECT_HEAD pObj;
    UINT16 level = 0;
    UINT8 quality = 0;
    UINT8 lock = 0;


	struct nim_device* nim_dev;
  	if(cur_tuner_idx == 0)
		nim_dev = g_nim_dev;
	else
		nim_dev = g_nim_dev2;	

	if(w->bType != OT_CONTAINER)
		return ;

	pObj = (POBJECT_HEAD)&sig_bar_level;
	pObj->frame.uLeft = W_L + B_L_OF_B;
	pObj->frame.uWidth = B_W_B;
	
	pObj = (POBJECT_HEAD)& sig_bar_quality;
	pObj->frame.uLeft = W_L + B_L_OF_B;
	pObj->frame.uWidth = B_W_B;
	
	pObj = (POBJECT_HEAD)&sig_txt_level;
	pObj->frame.uLeft = W_L + TXTL_L_OF_B;
		
	pObj = (POBJECT_HEAD)&sig_txt_quality;
	pObj->frame.uLeft = W_L + TXTL_L_OF_B;
	
	
	pObj = (POBJECT_HEAD)&g_win_signal;
	pObj->frame.uLeft = W_L_B;
	pObj->frame.uWidth = W_W_B;

	pObj->pRoot = w;
	
   	nim_get_lock(nim_dev, &lock);
#ifdef _S3281_	
	nim_get_AGC(nim_dev, &level);
#endif	
	nim_get_SNR(nim_dev, &quality);
    if(NULL != nim_dev->do_ioctl)
    {
      nim_dev->do_ioctl(nim_dev, NIM_DRIVER_GET_AGC, (UINT32)&level);
    }

	dem_signal_to_display(lock,(UINT8*)&level,&quality);	
	win_signal_set_level_quality(level,quality,lock);	
}


void win_signal_close(void)
{
	POBJECT_HEAD pObj;
	
	pObj = (POBJECT_HEAD)&g_win_signal;
	pObj->pRoot = NULL;
}
BOOL color_test_flag = 0;
BOOL win_signal_update(void)
{
	POBJECT_HEAD pObj;
	PROGRESS_BAR* bar;
	TEXT_FIELD*	  txt;

	char str[20];
	INT str_len;

#ifdef CI_PLUS_SUPPORT
	if(IsCIPlus_Menu_Exist())
	{
        return FALSE;
	}
#endif        

	pObj = (POBJECT_HEAD)&g_win_signal;
	if(pObj->pRoot == NULL)
		return FALSE;


	bar = &sig_bar_level;
	txt = &sig_txt_level_per;
	OSD_SetTextFieldContent(txt, STRING_NUM_PERCENT, OSD_GetProgressBarPos(bar));


	bar = &sig_bar_quality;
	txt = &sig_txt_quality_per;
	OSD_SetTextFieldContent(txt, STRING_NUM_PERCENT, OSD_GetProgressBarPos(bar));

	OSD_DrawObject(pObj, C_UPDATE_ALL);

#if 0
     struct OSDRect rect;
    VSCR vscr;
    int  i,j;
    extern struct osd_s3601_private *gp_osd_s3601_private0;
    UINT8        *pBuf,*pbyte;
    UINT32        *buf;
    //ge_s3601_private_t *osd_s3601_private0 = (ge_s3601_private_t *)(g_ge_dev->priv);
    UINT8 *ge_data_buf = (UINT8 *)(__MM_GE_START_ADDR);
    UINT8 *osd_bg_buf = (UINT8 *)((__MM_OSD_BK_ADDR1 + 31) & 0xFFFFFFE0);
    rect.uLeft = 240;
    rect.uTop  = 162;
    rect.uWidth  = 80;
    rect.uHeight = 160;
    pBuf = (UINT8 *)MALLOC(51200);
    MEMSET(pBuf,0,51200);
    vscr.bBlockID = 0;
    vscr.bColorMode = OSD_HD_ARGB8888;
    vscr.lpbScr = pBuf;
    vscr.updatePending = 1;
    vscr.vR = rect;
    if(color_test_flag)
    {
        libc_printf("\n===============================display buf======================================\n");
        for(i = 0; i < 2; i++)
        {
            pbyte = (UINT8 *)(ge_data_buf) + 5120 * (i+rect.uTop) + rect.uLeft * 4;//
            buf = (UINT32 *)pbyte;  
            for(j = 0;j < rect.uWidth;j++)
            {
                if(j % 8 == 0)
                {
                   libc_printf("\n"); 
                }
                libc_printf("0x%04x,",*buf++);
            }
        }
        libc_printf("\n=================================background buf=================================\n");
        for(i = 0; i < 2; i++)
        {
            pbyte = (UINT8 *)(osd_bg_buf) + 5120 * (i+rect.uTop) + rect.uLeft * 4;//
            buf = (UINT32 *)pbyte;  
            for(j = 0;j < rect.uWidth;j++)
            {
                if(j % 8 == 0)
                {
                   libc_printf("\n"); 
                }
                libc_printf("0x%04x,",*buf++);
            }
        }
        
    }
    while(color_test_flag)
    {
        MEMSET(pBuf,0,51200);
        OSDDrv_RegionRead(g_osd_dev,0,&vscr,&vscr.vR);
        libc_printf("\n----------------------------------------read virtual buf-------------------------------");
        for(i = 0; i < 2; i++)
        {
            pbyte = vscr.lpbScr;
            buf = (UINT32 *)pbyte;  
            for(j = 0;j < rect.uWidth;j++)
            {
                if(j % 8 == 0)
                {
                   libc_printf("\n"); 
                }
                libc_printf("0x%04x,",*buf++);
            }
        }
        libc_printf("\n----------------------------------------read background buf=-------------------------------");
        for(i = 0; i < 2; i++)
        {
            pbyte = (UINT8 *)(osd_bg_buf) + 5120 * (i+rect.uTop) + rect.uLeft * 4;//
            buf = (UINT32 *)pbyte;  
           
            for(j = 0;j < rect.uWidth;j++)
            {
                if(j % 8 == 0)
                {
                   libc_printf("\n"); 
                }
                libc_printf("0x%04x,",*buf++);
            }
        }
        osal_task_sleep(20);
        libc_printf("\n----------------------------------------write-------------------------------");
        OSDDrv_RegionWrite(g_osd_dev,0,&vscr,&vscr.vR);
        for(i = 0; i < 2; i++)
        {
            pbyte = (UINT8 *)(ge_data_buf) + 5120 * (i+rect.uTop) + rect.uLeft * 4;
            buf = (UINT32 *)pbyte;  
            for(j = 0;j < rect.uWidth;j++)
            {
                if(j % 8 == 0)
                {
                   libc_printf("\n"); 
                }
                libc_printf("0x%04x,",*buf++);
            }
        }
        libc_printf("\n----------------------------------------End-------------------------------");
        for(i = 0; i < 2; i++)
        osal_task_sleep(100);
    }
#endif
	return TRUE;
}


void win_signal_refresh(void)
{    
    UINT16 level = 0;
    UINT8 quality = 0;
    UINT8 lock = 0;
	struct nim_device* nim_dev;
	
  	if(g_tuner_num == 1)
		cur_tuner_idx = 0;

  	if(cur_tuner_idx == 0)
		nim_dev = g_nim_dev;
	else
		nim_dev = g_nim_dev2;	
	nim_get_lock(nim_dev, &lock);
#ifdef _S3281_	
	nim_get_AGC(nim_dev, &level);
#endif	
	nim_get_SNR(nim_dev, &quality);
	if(NULL != nim_dev->do_ioctl)
    {
      nim_dev->do_ioctl(nim_dev, NIM_DRIVER_GET_AGC, (UINT32)&level);
    }
#if (AOV_SIGNAL_CALCULATE_ENABLE == SYS_FUNC_OFF)
    dem_signal_to_display(lock,(UINT8*)&level,&quality);
#endif
    win_signal_set_level_quality(level,quality,lock);
    
	win_signal_update();
}

#ifdef DISPLAY_SIGNAL_QUALITY_EXT
static const UINT32 agc_table[256] = {
	  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   1,   2,   3,   4,   5, \
  6,   7,   8,   9,  10,  11,  12,  13,  14,  15,  16,  17,  18,  19,  20,  21, \
 22,  23,  24,  25,  26,  27,  28,  29,  30,  31,  32,  33,  34,  35,  36,  37, \
 38,  39,  40,  41,  42,  43,  44,  45,  46,  47,  48,  49,  50,  52,  55,  57, \
 60,  62,  65,  67,  70,  72,  75,  77,  78,  79,  80,  82,  83,  84,  85,  87, \
 88,  89,  90,  92,  93,  94,  95,  97,  98,  99, 101, 102, 103, 104, 105, 106, \
107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, \
123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, \
139, 140, 141, 142, 144, 145, 146, 147, 149, 150, 151, 152, 154, 155, 156, 157, \
159, 160, 161, 162, 164, 165, 166, 167, 169, 170, 171, 173, 175, 176, 178, 180, \
182, 184, 187, 189, 192, 194, 197, 199, 202, 204, 207, 209, 212, 215, 218, 222, \
225, 228, 232, 235, 238, 242, 245, 248, 252, 255, 258, 262, 265, 268, 272, 275, \
278, 282, 285, 288, 292, 295, 298, 302, 305, 308, 312, 317, 322, 327, 332, 337, \
342, 347, 352, 357, 362, 367, 372, 377, 382, 387, 392, 397, 402, 407, 412, 417, \
422, 427, 432, 437, 442, 447, 452, 457, 462, 467, 472, 477, 482, 487, 492, 493, \
494, 495, 496, 497, 498, 499, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, \
};
#endif

void dem_signal_to_display(UINT8 lock,UINT8* level,UINT8* quality)
{
#ifdef DISPLAY_SIGNAL_QUALITY_EXT
	#if ((defined _S3811_)&&(defined DVBT_SUPPORT||defined ISDBT_SUPPORT))
	    return;
	#else
		UINT32 temp;
		temp = agc_table[(*level)];
		*level = (UINT8)(temp/5);
		if (lock){
			temp = *quality;
			if (temp<0)
				temp = 0;
			if (temp>100)
				temp = 100;
			*quality = temp;
		}
	    else
			*quality = 0;
	#endif
#else
    #if (defined(ACCURATE_SSI_SQI))//accurate value from demod,don't modify it 
		struct nim_device* nim_dev;
		if(g_tuner_num == 1)
			cur_tuner_idx = 0;
		if(cur_tuner_idx == 0)
			nim_dev = g_nim_dev;
		else
			nim_dev = g_nim_dev2;
		if(FRONTEND_TYPE_T == dev_get_device_sub_type(nim_dev, HLD_DEV_TYPE_NIM))
		{
		/*
			if(*level>90)
	   			*level = 90;
			if(*quality>90)
				*quality = 90;
		*/
			if(!lock)
			{
				*level= 0;
				*quality = 0;
			}
			return;
		}
	#endif

	if(*level>90)
	   *level = 90;
	if (*level < 10)
	   *level = *level * 11/2;
	else 
	   *level = *level / 2 + 50;       
	if(*level>90)
	   *level = 90;

	if(!lock)
	   *level /= 2;
 	if(lock)    
 	{
 	    if (*quality < 30)
 	        *quality = *quality * 7/ 3;
 	    else
 	        *quality = *quality / 3 + 60; 
 	}
 	else
 	{
 		if (*quality >= 20)
 			*quality =  *quality * 3/ 7;
  	}
 	if(*quality>90)
 		*quality = 90;
#endif
}


